var setting ={
    showList: true,
    page: {
        pageNo: 10,
        total: 20,
        pageSize: 10,
        dataList: []
    },
    queryParam: {
        user_name: '',
    },
    text: {
        submitButtonText:'添加',
    },
    options: [],
    selectedData: [],
    form: {
        uid:'',
        user_name:'',
        real_name: '',
        contact_way: '',
        sex:0
    },
    roles: [],
    allRoles:[],
    rules: {
        user_name: [
            { required: true, message: '请输入名称', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
        ]
    },
    loading: false,
    statusEnum: [{text: '启用', value: '1'},{text: '禁用', value: '0'}],
    sexEnum: [{text: '女', value: '1'},{text: '男', value: '0'}],
    isAdd: true,
}
var vue = new Vue({
    el: '.main',
    data: setting,
    methods: {
        handleCurrentChange: function (pageNo) {//分页查询
            page(pageNo)
        },
        handleSizeChange: function (pageSize) {//修改pageSize查询
            vue.page.pageSize=pageSize
            page(1)
        },
        query: function () {//条件查询
            page(1)
        },
        getSelected: function (rows) {//获取选中内容
            vue.selectedData = rows;
        },
        changeAddOrEdit: function (type) {//切换添加
            if(type == 'add'){
                vue.text.submitButtonText = '添加';
                vue.isAdd = true;
            }else {
                vue.text.submitButtonText = '修改';
                var selectedData = vue.selectedData;
                if(selectedData.length == 0){
                    vue.$message.warning( '请选择要修改的数据');
                    return ;
                }else if(selectedData.length > 1){
                    vue.$message.warning( '请选择一条要修改的数据');
                    return ;
                }else {
                    vue.isAdd = false;
                    $.post('/user/getUserById',{uid: selectedData[0].uid}, function (user) {
                        vue.form = user;
                    })
                }
            }
            $.post('/user/getAllRoles',function (roles) {
                vue.allRoles = roles;
            })
            vue.showList = false;
        },
        resetForm: function () {
            this.$refs.form.resetFields();
        },
        goBack: function () {
            vue.showList = true;
            vue.resetForm();
        },
        addOrEdit: function () {
            vue.loading = true;
            vue.$refs['form'].validate(function (v) {
                if(v){
                    var data = vue.form;
                    var url = (vue.isAdd?'/user/add':'/user/edit');
                    data.uid = data.uid == ''?0:data.uid;
                    vue.roles.forEach(function (item, index) {
                        data['roles['+index+'].role_id']=item;
                    })
                    $.post(url, data, function (msg) {
                        vue.loading = false;
                        if(msg.status == 'SUCCESS'){
                            vue.$message.success(msg.message)
                            vue.showList = true;
                            page(vue.page.pageNo)
                            vue.resetForm();
                            vue.showPassword = true;
                        }else{
                            vue.$message.error(msg.message);
                        }
                    })
                }
            })
        },
        filterStatus: function (value, row) {
            return row.type == value;
        },
        filterMethod: function (query, item) {
            return item.name.indexOf(query) > -1;
        },
        initPassword: function (uid) {
            $.post("/user/initPassword", {uid: uid}, function (msg) {
                if(msg.status == 'SUCCESS'){
                    vue.$message.success(msg.message)
                }else{
                    vue.$message.error(msg.message);
                }
            })
        },
        updateStatus: function (uid, status) {
            $.post("/user/updateStatus", {uid: uid, status: status}, function (msg) {
                if(msg.status == 'SUCCESS'){
                    vue.$message.success(msg.message)
                }else{
                    vue.$message.error(msg.message);
                }
            })
        }
    }
})
page(1)
function page(page) {
    var data = {
        page: page,
        pageSize: vue.page.pageSize,
        title: vue.queryParam.title,
    }
    $.post('/user/queryByPage', data, function (page) {
        vue.page.dataList = page.rows;
        vue.page.pageNo = page.page;
        vue.page.total = page.total;
    })
}